package com.hzyl.famousreader.view.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.umeng.analytics.MobclickAgent;
import com.hzyl.famousreader.Constant;
import com.hzyl.famousreader.R;
import com.hzyl.famousreader.databinding.ActivityRegisterBinding;
import com.hzyl.famousreader.repository.webservice.RetrofitClient;
import com.hzyl.famousreader.repository.webservice.api.UserService;
import com.hzyl.famousreader.repository.webservice.model.Register;
import com.hzyl.famousreader.repository.webservice.model.Result;
import com.hzyl.famousreader.router.Router;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 注册页
 */
@Route(path = Router.ACTIVITY_REGISTER, name = "注册页")
public class RegisterActivity extends AppCompatActivity {

  private ActivityRegisterBinding binding;

  private Register register;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    binding = DataBindingUtil.setContentView(this, R.layout.activity_register);

    BarUtils.setStatusBarColor(this, getResources().getColor(R.color.bg_sky_blue), 0);

    register = new Register();
    binding.setRegister(register);

    binding.checkPhone.setOnClickListener(v -> {
      if (!StringUtils.isTrimEmpty(register.getPhone())
        && !StringUtils.isTrimEmpty(register.getPassword())
        && !StringUtils.isTrimEmpty(register.getRepassword())) {
        binding.register1.setVisibility(View.GONE);
        binding.register2.setVisibility(View.VISIBLE);
      } else {
        ToastUtils.showShort("请先完善信息");
      }
    });

    binding.sendvcode.setOnClickListener(v -> {
      new CountDownTimer(30000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
          binding.sendvcode.setEnabled(false);
          binding.sendvcode.setText("已发送(" + millisUntilFinished / 1000 + ")");
        }

        @Override
        public void onFinish() {
          binding.sendvcode.setEnabled(true);
          binding.sendvcode.setText("重新获取");
        }
      }.start();

      if (!StringUtils.isTrimEmpty(register.getPhone())) {
        UserService userService = RetrofitClient.getInstance().create(UserService.class);
        userService.sendSms(register.getPhone(), Constant.SMS_REGISTER_TEMPLATE).enqueue(new Callback<Result>() {
          @Override
          public void onResponse(Call<Result> call, Response<Result> response) {
            Result result = response.body();
            if (result != null && result.getErrno() == Result.SUCCESS_ERRNO) {
              ToastUtils.showShort("获取短信验证码成功");
            } else {
              ToastUtils.showShort("获取短信验证码失败，请重试");
            }
          }

          @Override
          public void onFailure(Call<Result> call, Throwable t) {
            LogUtils.d("获取短信验证码异常");
          }
        });
      } else {
        ToastUtils.showShort("请先输入手机号");
      }
    });

    binding.toLoginPage.setOnClickListener(v -> ARouter.getInstance().build(Router.ACTIVITY_LOGIN).navigation());

    binding.checkCode.setOnClickListener(v -> {
      UserService userService = RetrofitClient.getInstance().create(UserService.class);
      userService.register(register.getPhone(), register.getPassword(), register.getRepassword(), register.getSmsCode())
        .enqueue(new Callback<Result>() {
          @Override
          public void onResponse(Call<Result> call, Response<Result> response) {
            Result result = response.body();
            if (result != null && result.getErrno() == Result.SUCCESS_ERRNO) {
              ToastUtils.showShort("注册成功");
              ARouter.getInstance().build(Router.ACTIVITY_LOGIN).navigation();
            } else {
              ToastUtils.showShort(result.getErrmsg());
            }
          }

          @Override
          public void onFailure(Call<Result> call, Throwable t) {

          }
        });
    });
  }

  @Override
  protected void onPause() {
    super.onPause();
    MobclickAgent.onPageEnd("注册页");
    MobclickAgent.onPause(this);
  }

  @Override
  protected void onResume() {
    super.onResume();
    MobclickAgent.onPageStart("注册页");
    MobclickAgent.onResume(this);
  }
}
